package daoImpl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import bean.User;
import dao.UserDao;
import util.HibernateUtils;

public class UserDaoImpl implements UserDao {
	HibernateUtils hu = new HibernateUtils();
	
	@Override
	public boolean finduser(User user) {
		Session session = hu.getSession();
		String hql = "from User where name=:name and password=:password";
		
		Query query = session.createQuery(hql);
		query.setParameter("name", user.getName());
		query.setParameter("password", user.getPassword());
		List<User> u = query.list();
		hu.closeSession(session);
		if(u.size() > 0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean reguser(User user) {
		Session session=hu.getSession();
		session.beginTransaction();
		String hql="from User where name=:n";
		Query query=session.createQuery(hql);
		query.setParameter("n", user.getName());
		List<User> u=query.list();
		if(u.isEmpty()){
			session.save(user);
			session.getTransaction().commit();
			hu.closeSession(session);
			return true;
		}else{
	    hu.closeSession(session);
		return false;}
	}

//	@Override
//	public boolean writeuser(User user) {
//		Session session = hu.getSession();
//		session.beginTransaction();
//		
//        String hql=" from User where name=:n";
//		Query query = session.createQuery(hql);
//		query.setParameter("n", user.getName());
//		List<User> u = query.list();
//		if(u.isEmpty()){
//			session.save(user);
//			session.getTransaction().commit();
//			hu.closeSession(session);
//			return true;
//		}else{
//			hu.closeSession(session);
//			return false;
//		}
//	}
}